﻿using System;
using System.IO;

namespace CommonCommandsPlugin
{
    internal class FolderStructure
    {
        public static string Get(string path, bool getSize, int? maxdepth)
        {
            return _Structure(path, getSize, 0, maxdepth ?? DEFAULT_MAX_DEPTH);
        }

        private static string _Structure(string path, bool getSize, int currentDepth, int maxdepth)
        {
            try
            {
                var directories = Directory.GetDirectories(path);
                string message = Environment.NewLine +
                    path.PadLeft(path.Length + currentDepth * 4, ' ');

                if (currentDepth != maxdepth)
                {
                    foreach (var directory in directories)
                    {
                        if (directory.Contains("Windows"))
                            continue;
                        message += _Structure(directory, getSize, currentDepth + 1, maxdepth);
                    }
                }

                return message;
            }
            catch (UnauthorizedAccessException ex)
            {
                return null;
            }
        }

        private const int TAB = 4;
        private const int DEFAULT_MAX_DEPTH = 5;
    }
}
